package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo< Statement > query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }

    /**
     * 1创建时间是后台来完成添加的
     * 2日期无法封装 实体类贴注解@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
     * 3实际到店时间是不用填写的
     * @param statement
     */
    @Override
    public void save(Statement statement) {
        statement.setCreateTime(new Date());

        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement dbApponitment = this.get(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(dbApponitment.getStatus())) {
            throw  new BusinessException("非法操作");
        }
        dbApponitment.setCustomerName(statement.getCustomerName());
        dbApponitment.setCustomerPhone(statement.getCustomerPhone());
        dbApponitment.setActualArrivalTime(statement.getActualArrivalTime());
        dbApponitment.setLicensePlate(statement.getLicensePlate());
        dbApponitment.setCarSeries(statement.getCarSeries());
        dbApponitment.setServiceType(statement.getServiceType());
        dbApponitment.setInfo(statement.getInfo());
        statementMapper.updateByPrimaryKey(dbApponitment);
    }


    /**
     *要删未结算的账单明细,就要删除中间的联系表,以及将预约的信息改为到店,可以让他重新发起账单明细表
     */
    @Override
    public void deleteBatch(Long[] ids) {

        for (Long dictId : ids) {
            Statement statement = statementMapper.selectByPrimaryKey(dictId);
            statementMapper.deleteByPrimaryKey(dictId);
            statementItemMapper.deleteItem(dictId);
            Long appointmentId = statement.getAppointmentId();
            if (appointmentId!=null) {
               appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_ARRIVAL);
            }
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }



}
